package com.dodol.common;

import com.dodol.util.ElementFinder;
import org.openqa.selenium.*;

import org.openqa.selenium.interactions.Actions;


import org.apache.commons.io.FileUtils;
import org.openqa.selenium.support.ui.Select;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 测试步骤中执行的动作
 */
public enum UserAction {


    CLICK("点击", 0),   // 点击
    PRESS_KEY("输入", 1),   // 输入值
    CLEAR("清空", 2),   // 清空内容
    OPEN("打开", 3),   // 打开网址
    CLOSE("关闭", 4),   //关闭
    BACK("后退", 5),    //后退
    FORWARD("前进", 6), //前进
    REFRESH("刷新", 7), //刷新
    SUBMIT("提交", 8),  //提交，回车
    DOUBLE_CLICK("双击", 9),    //双击
    CONTEXT_CLICK("右键单击", 10),  //右键单击
    CLICK_AND_HOLD("点击控制", 11), // 鼠标按住
    DRAG_DROP("拖拽", 12),  //鼠标拖拽
    SELECT("下拉选择", 13);  //下拉选中

    private String action;
    private int type;


    UserAction(String action, int type) {
        this.action = action;
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public String getAction() {
        return action;
    }

    /**
     * 截屏操作
     *
     * @param driver
     * @return
     * @throws InterruptedException
     */
    public static String getScreenShot(WebDriver driver) throws InterruptedException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");  //转换时间格式
        String time = dateFormat.format(Calendar.getInstance().getTime());  //获取当前时间
        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);  //执行屏幕截取
        try {
            FileUtils.copyFile(srcFile, new File(".\\屏幕截图", time + ".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Thread.sleep(2000);
        return ".\\屏幕截图\\" + time + ".png";

    }

    /**
     * 上传文件
     *
     * @param driver
     * @param filePath
     */
    public static void uploadFile(WebDriver driver, String filePath) {
        //定位上传按钮， 添加本地文件
        driver.findElement(By.name("file")).sendKeys(filePath);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static class MouseAction {

        /**
         * 下拉列表操作
         *
         * @param driver
         * @param step
         */
        public static void selectElement(WebDriver driver, TestStep step) {

            WebElement element = ElementFinder.findElement(driver, step.getElement());
            assert element != null;
            Select select = new Select(element);
            select.selectByValue(step.getEnterValue());
        }


        /**
         * 右键单击某个元素
         *
         * @param driver
         * @param step
         */
        public static void clickAndHoldElement(WebDriver driver, TestStep step) {
            WebElement element = ElementFinder.findElement(driver, step.getElement());
            assert element != null;
            Actions actions = new Actions(driver);
            actions.clickAndHold(element).perform();
        }

        /**
         * 右键单击某个元素
         *
         * @param driver
         * @param step
         */
        public static void contextClickElement(WebDriver driver, TestStep step) {
            WebElement element = ElementFinder.findElement(driver, step.getElement());
            assert element != null;
            Actions actions = new Actions(driver);
            actions.contextClick(element).perform();
        }


        /**
         * 双击元素动作
         *
         * @param driver
         * @param step
         */
        public static void doubleClickElement(WebDriver driver, TestStep step) {
            WebElement element = ElementFinder.findElement(driver, step.getElement());
            assert element != null;
            Actions actions = new Actions(driver);
            actions.doubleClick(element).perform();
        }


        /**
         * 拖拽元素
         *
         * @param driver
         * @param source 起始位置
         * @param target 终点位置
         */
        public static void dragDropElement(WebDriver driver, WebElement source, WebElement target) {
            // 鼠标拖拽动作，将 source 元素拖放到 target 元素的位置。
            Actions actions = new Actions(driver);
            actions.dragAndDrop(source, target).perform();
            // 释放鼠标
            actions.release().perform();
        }

        /**
         * 点击动作
         *
         * @param step 测试步骤
         * @return
         */
        public static void clickElement(WebDriver driver, TestStep step) {
            WebElement element = ElementFinder.findElement(driver, step.getElement());
            if (element != null) {
                element.click();
            } else {
                throw new NoSuchElementException("无法定位到元素：" + step.getElement().toString());
            }
        }


    }

    public static class KeyboardAction {
        /**
         * sendKeys(Keys.BACK_SPACE) 回格键（BackSpace）
         * sendKeys(Keys.SPACE) 空格键(Space)
         * sendKeys(Keys.TAB) 制表键(Tab)
         * sendKeys(Keys.ESCAPE) 回退键（Esc）
         * sendKeys(Keys.ENTER) 回车键（Enter）
         * sendKeys(Keys.CONTROL,‘a’) 全选（Ctrl+A）
         * sendKeys(Keys.CONTROL,‘c’) 复制（Ctrl+C）
         * sendKeys(Keys.CONTROL,‘x’) 剪切（Ctrl+X）
         * sendKeys(Keys.CONTROL,‘v’) 粘贴（Ctrl+V）
         * sendKeys(Keys.F1) 键盘 F1
         */


        /**
         * 清空动作
         *
         * @param step
         */
        public static void clearElement(WebDriver driver, TestStep step) {

            WebElement element = ElementFinder.findElement(driver, step.getElement());
            if (element != null) {
                element.clear();
            } else {
                throw new NoSuchElementException("无法定位到元素：" + step.getElement().toString());
            }
        }


        /**
         * 输入内容
         *
         * @param driver
         * @param step
         */
        public static void pressKeyInElement(WebDriver driver, TestStep step) {
            WebElement element = ElementFinder.findElement(driver, step.getElement());

            if (element != null) {
                element.clear();
                element.sendKeys(step.getEnterValue());
            } else {
                throw new NoSuchElementException("无法定位到元素：" + step.getElement().toString());
            }
        }

        /**
         * 在元素中提交内容
         *
         * @param driver
         * @param step
         */
        public static void submitElement(WebDriver driver, TestStep step) {
            WebElement element = ElementFinder.findElement(driver, step.getElement());
            if (element != null) {
                element.submit();
            } else {
                throw new NoSuchElementException("无法定位到元素：" + step.getElement().toString());
            }
        }


    }


}
